package com.gz.steak.utils.convert;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.beanutils.BeanUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author 刘牌
 * @Date 2020/3/19 0019 21:05
 * @Version 1.0
 */

public class ConvertUtil {

    /**
     * 集合转JSON
     * @param list
     * @param <T>
     * @return
     */
    public static <T> Object ListToJson(List<T> list){
        return JSONObject.toJSON(list);
    }

    /**
     * 格式化日期
     * @param date
     * @return
     */
    public static Date FormatDate(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = simpleDateFormat.format(date);
        Date newDate = null;
        try {
            newDate = simpleDateFormat.parse(s);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return newDate;

    }

    public static String DateToString(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static Object modelToVo(Object vo , Object model){
        try {
            BeanUtils.copyProperties(vo,model);
        }catch (Exception e){
            e.printStackTrace();
        }
        return vo;
    }
}
